//
//  RBDZJokeViewController.m
//  rubaoduanzi
//
//  Created by 李云飞 on 16/9/13.
//  Copyright © 2016年 liyunfei. All rights reserved.
//

#import "RBDZJokeViewController.h"
#import "RBDZButton.h"
#import <AFNetworking.h>
#import "RBDZJokeTableViewCell.h"
@interface RBDZJokeViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
//保存请求的数据
@property(nonatomic,strong)NSMutableArray *arrayJoke;
@property(nonatomic,assign)CGFloat cellHeight;
@property(nonatomic,strong)UIImageView *img;
@end

@implementation RBDZJokeViewController
//static NSString *ID=@"cell";
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=@"儒豹笑话";
    [self addItem];
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    [self loadData];
    //[self.tableView registerNib:[UINib nibWithNibName:@"RBDZJokeTableViewCell" bundle:nil] forCellReuseIdentifier:ID];
    
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.arrayJoke.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ID=@"cell";
    RBDZJokeModel *model=self.arrayJoke[indexPath.row];
    RBDZJokeTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:ID];
    if (cell==nil) {
        cell=[RBDZJokeTableViewCell jokeTableViewCell];
    }
    cell.jokeModel=model;
    self.cellHeight=CGRectGetMaxY(cell.titleLabel.frame)+CGRectGetMaxY(cell.labelText.frame)+Margin+cell.zanBtn.frame.size.height;
    //NSLog(@"%@",cell);
    if ([model.type isEqualToString: @"detail"]) {
        cell.imgZD.hidden=NO;
    }else{
        cell.imgZD.hidden=YES;
    }
    return cell;
}
#pragma mark 返回每行的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//    RBDZJokeTableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];
//    CGFloat cellHeight=CGRectGetMaxY(cell.titleLabel.frame)+CGRectGetMaxY(cell.labelText.frame)+cell.zanBtn.frame.size.height+Margin;
//    return cellHeight;
    return self.cellHeight;
}
#pragma mark 加载网络数据
-(void)loadData{
    AFHTTPSessionManager *manager=[AFHTTPSessionManager manager];
    //置顶数据
    NSString *url1=@"http://mobileapi1.roboo.com/joke/stickJson.do?cat=joke";
    //置顶以外的数据
    NSString *url2=@"http://mobileapi1.roboo.com/joke/textJokeJson.do?p=1&ps=14";

    [manager GET:url1 parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //NSLog(@"%@",responseObject);
        NSArray *array=responseObject[@"items"];
        for (NSDictionary *dict in array) {
            RBDZJokeModel *model=[RBDZJokeModel jokeModelWithDict:dict];
            [self.arrayJoke addObject:model];
        }
        //NSLog(@"%@",self.arrayJoke);
        //[self.tableView reloadData];
        //[array writeToFile:@"/Users/liyunfei/Desktop/火影忍者/1234.plist" atomically:YES];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"网络错误");
    }];
    [manager GET:url2 parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //[responseObject writeToFile:@"/Users/liyunfei/Desktop/火影忍者/2234.plist" atomically:YES];
        NSArray *array=responseObject[@"items"];
        for (NSDictionary *dict in array) {
            RBDZJokeModel *model=[RBDZJokeModel jokeModelWithDict:dict];
            [self.arrayJoke addObject:model];
        }
        //NSLog(@"%@",array);
        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"网络错误");
    }];
}
#pragma mark 添加导航栏左右按钮
-(void)addItem{
    UIButton *buttonL=[UIButton buttonWithType:UIButtonTypeCustom];
    self.navigationItem.leftBarButtonItem=[RBDZButton barButtonItemWith:buttonL andTarget:self action:@selector(leftBtn) andImageName:@"nav_menu" andHightlightImage:nil];
    UIButton *buttonR=[UIButton buttonWithType:UIButtonTypeCustom];
    self.navigationItem.rightBarButtonItem=[RBDZButton barButtonItemWith:buttonR andTarget:self action:@selector(rightBtn) andImageName:@"nav_fb" andHightlightImage:@"nav_fb_pre"];
    
}
#pragma mark 添加左边按钮监听事件
-(void)leftBtn{
    RBDZMEViewController *me=[[RBDZMEViewController alloc]init];
    me.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:me animated:YES];
}
#pragma mark 添加右边按钮监听事件
-(void)rightBtn{
    //NSLog(@"%s",__func__);
    RBDZContributeViewController *contribute=[[RBDZContributeViewController alloc]init];
    [self.navigationController pushViewController:contribute animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(NSMutableArray *)arrayJoke{
    if (!_arrayJoke) {
        _arrayJoke=[NSMutableArray array];
    }
    return _arrayJoke;
}
-(UIImageView *)img{
    if (!_img) {
        _img=[[UIImageView alloc]init];
    }
    return _img;
}

@end
